package com.fivefu.core.data.properties;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.fivefu.base.common.utils.str.StrUtils;
import com.fivefu.base.web.vo.ResultInfo;
import com.fivefu.okhttpbase.OkHttpClientUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

import java.util.Iterator;
import java.util.Map;

@Component
@ConfigurationProperties(prefix = "stream-service")
public class StreamServiceMapProperties {

    @Autowired
    private OkHttpClientUtil okHttpClientUtil;

    /**
     * 实时流播放地址服务
     */
    private Map<String,StreamServiceProperties> previewUrlService;

    /**
     * 录像流播放地址服务
     */
    private Map<String,StreamServiceProperties> recordUrlService;

    /**
     * 播放地址截图服务
     */
    private Map<String,StreamServiceProperties> snapeVideoService;

    public Map<String, StreamServiceProperties> getPreviewUrlService() {
        return previewUrlService;
    }

    public void setPreviewUrlService(Map<String, StreamServiceProperties> previewUrlService) {
        this.previewUrlService = previewUrlService;
    }

    public Map<String, StreamServiceProperties> getRecordUrlService() {
        return recordUrlService;
    }

    public void setRecordUrlService(Map<String, StreamServiceProperties> recordUrlService) {
        this.recordUrlService = recordUrlService;
    }

    public Map<String, StreamServiceProperties> getSnapeVideoService() {
        return snapeVideoService;
    }

    public void setSnapeVideoService(Map<String, StreamServiceProperties> snapeVideoService) {
        this.snapeVideoService = snapeVideoService;
    }

    /**
     * 请求服务
     * @param streamService 请求服务信息
     * @param paramMap 请求参数
     *
     * 通过固定参数，请求指定接口
     * 相应结构为固定对象ResultInfo,响应值为链接
     *      eg => 调用实时流播放地址服务 => 响应结果为 实时流播放地址
     *
     * @return
     */
    public String queryStreamService(StreamServiceProperties streamService, Map<String, String> paramMap){
        if (streamService.getEnable()){
            String result = null;
            if (streamService.getRequestMethod().toUpperCase().equals("POST")){
                try {
                    result = okHttpClientUtil.Post(streamService.getServiceUrl(),paramMap);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            } else if (streamService.getRequestMethod().toUpperCase().equals("GET")){
                String param = "";
                Iterator<String> it = paramMap.keySet().iterator();
                while (it.hasNext()){
                    if (StrUtils.isNull(param)){
                        param += "?";
                    }else{
                        param += "&";
                    }
                    String key = it.next();
                    String val = paramMap.get(key);
                    param += key + "=" + val;
                }
                try {
                    result = okHttpClientUtil.Get(streamService.getServiceUrl() + param);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            if (StrUtils.isNotNull(result)){
                ResultInfo resultInfo = JSON.parseObject(result, ResultInfo.class);
                if (resultInfo.getCode().equals(200)){
                    if (ObjectUtils.isNotEmpty(resultInfo.getData())){
                        return resultInfo.getData().toString();
                    }
                    if (StrUtils.isNotNull(resultInfo.getMsg())){
                        return resultInfo.getMsg();
                    }
                }
            }
        }
        return null;
    }
}
